<?php
include_once 'lib/config.php';
include_once 'lib/db.php';

function inject_head(){?>
<script type="text/javascript" src="jscript/Spinner.js"></script>
<link rel="stylesheet" type="text/css" href="css/Spinner.css" />
<script>
	var app = {
		url:'proc/admin/nilai.php'};
	
	/**
	 * update table rewarddanpunishment
	 */
	var orderby_old = null;
	var orderby_type = ['ASC', 'DESC'];
	var orderby_count = 0;
	function updateList(key, orderby){
		if (orderby==orderby_old) orderby_count++;	
		else orderby_count=0;
		if ($chk(orderby)){
			orderby_str = orderby+" "+orderby_type[orderby_count%2];
			orderby_old = orderby;
		}
		else{
			orderby_str = "";
			orderby_old = "";
		}
		key = key==null? "" : key;
		doRequest(app.url, 'post', 'proc=nilai-table&key='+key+'&orderby='+orderby_str, 
			function (res){ 
//				alert(res);
				$('nilai-table').innerHTML = res;
		});
	}

	//insert a record to table
	function doAdd() {
		FBModal_show2(app.url, 'post', "proc=add-modal", true, true, null, {onSuccess:function(){
			spinner_attach('nilai_point', 'nilai_point', 0);
			spinner_attach('nilai_min', 'nilai_min', 1, null, 1);
			spinner_attach('nilai_max', 'nilai_max', 1, null, 1);
		}});
	}

	function doSave(form){
		FBModal_loading("Save", "Please wait...", true, false);
		$(form).set('send', {
			onSuccess: function(response) { 
				//alert(response);
				var js = JSON.decode(response);
				var msg = js.error? js.msg : "Process simpan selesai !!!";
				var title = js.error? 'Error' : 'Saving';
				FBModal_show(
					"<h2 class=\"dialog_title\"><span>"+title+"</span></h2>" + 
					"<div class=\"dialog_content\" style=\"padding: 10px 20px\">"+msg+"</div>",
					true, true);

				updateList();
			}
		}).send();
	}

	function doEdit(id){
		FBModal_show2( app.url, 'post', "proc=edit-modal&nilai_id="+id, true, true, null, {onSuccess:function(){
			spinner_attach('nilai_point', 'nilai_point', 0, null, $('nilai_point').getProperty('currentValue'));
			spinner_attach('nilai_min', 'nilai_min', 1, null, $('nilai_min').getProperty('currentValue'), null, null);
			spinner_attach('nilai_max', 'nilai_max', 1, null, $('nilai_max').getProperty('currentValue'), null, null);
		}});
	}

	function doDelete(id){
		FBModal_loading("Delete", "Please wait...", false, false);
		doRequest(app.url, 'post', 'proc=nilai-delete&nilai_id='+id, 
			null,
			function(res){
				//update tablenya
				updateList();
				
				var js = JSON.decode(res);
				var msg = js.error? js.msg : "Process penghapusan selesai !!!";
				var title = js.error? 'Error' : 'Delete';
				
				FBModal_show(
					"<h2 class=\"dialog_title\"><span>"+title+"</span></h2>" + 
					"<div class=\"dialog_content\" style=\"padding: 10px 20px\">"+msg+"</div>",
					true, true);
			});
	}
	
	function doAdd_bidangCombo(jenis, target){
		doRequest(app.url, 'post', 'proc=nilai-add-combo-bidang&achiev_jenis='+jenis, null,
			function(res){ 
				target.set('html',res);
				doAdd_tingkatCombo(jenis, target.value, $('detachiev_tingkat'));
			});
		$('reward_jenis').item(0).disabled= !(jenis=="prestasi");
		$('reward_jenis').item(1).disabled= !(jenis!="prestasi");
		$('reward_jenis').selectedIndex = jenis=="prestasi"? 0:1;
		doAdd_rewardCombo($('reward_jenis').item(jenis=="prestasi"? 0:1).value, $('reward_id'));
	}
	
	function doAdd_tingkatCombo(jenis, bidang, target){
		doRequest(app.url, 'post', 'proc=nilai-add-combo-tingkat&achiev_bidang='+bidang+'&achiev_jenis='+jenis, null,
			function(res){ 
				target.set('html',res); 
				doAdd_kategoriCombo(jenis, bidang, target.value, $('detachiev_id')); 
			});
	}
	
	function doAdd_kategoriCombo(jenis, bidang, tingkat, target){
		doRequest(app.url, 'post', 'proc=nilai-add-combo-kategori&achiev_bidang='+bidang+'&achiev_jenis='+jenis+'&detachiev_tingkat='+tingkat, null,
			function(res){ target.set('html',res); });
	}
	
	function doAdd_rewardCombo(jenis, target){
		doRequest(app.url, 'post', 'proc=nilai-add-combo-reward&rnp_jenis='+jenis, null,
			function(res){ target.set('html',res); });
	}

	window.addEvent('domready', function(){ updateList(); });
</script><?php
}

include 'view/header.php';
include 'view/admin/nilai/nilai.php';
include 'view/footer.php';